package me.yricky.chart.base

import kotlin.math.max
import kotlin.math.min

typealias DataRange = Pair<Double,Double>
fun DataRange(min:Number,max:Number) = DataRange(min.toDouble(),max.toDouble())

val ZeroRange = DataRange(0.0,0.0)

inline val DataRange.min get() = first
inline val DataRange.max get() = second
inline val DataRange.valid get() = min < max

fun DataRange.contains(num:Number) = num.toDouble() < max && num.toDouble() > min

fun Number.relativeLocationIn(range:DataRange) = if(range.valid){
    (toDouble() - range.min) / (range.max - range.min)
} else{ 0.5 }

fun Collection<DataRange>.merge():DataRange = if(isEmpty()) ZeroRange else reduce{ dr1,dr2 ->
    DataRange(min(dr1.min,dr2.min), max(dr1.max,dr2.max))
}